<template>
  <div class="login-bg">
    <div class="login-wrapper">
      <!-- 左侧宣传栏 -->
      <div class="login-left">
        <div class="brand">
          <img src="@/assets/lgo2222.png" alt="logo" class="brand-logo" />
        </div>
        <h2 class="main-title">{{ $t("接受免费付费调查！") }}</h2>
        <h3 class="sub-title">
          {{ $t("兑换免费PaypalCash，") }}<br />
          {{ $t("比特币和礼品卡！") }}
        </h3>
        <p class="desc">
          {{ $t("从200多种奖励中选择，包括") }}<br />
          {{ $t("亚马逊礼品卡、比特币和PayPal现金") }}
        </p>
        <button class="watch-btn">
          <span class="watch-icon">▶</span> {{ $t("观看以开始") }}
        </button>
        <div class="awards">
          <div class="awards-title">{{ $t("特色奖项") }}</div>
          <div class="awards-list">
            <!-- 奖品图标，替换为实际图片路径 -->
            <img src="@/assets/award1.png" alt="award" />
            <img src="@/assets/award2.png" alt="award" />
            <img src="@/assets/award3.png" alt="award" />
            <img src="@/assets/award4.png" alt="award" />
            <img src="@/assets/award5.png" alt="award" />
            <img src="@/assets/award6.png" alt="award" />
          </div>
        </div>
      </div>
      <!-- 右侧登录卡片 -->
      <div class="login-right">
        <div class="card">
          <div class="card-title">{{ $t("加入并开始赚钱") }}</div>
          <div class="welcome-bonus">{{ $t("100分欢迎奖金") }}</div>
          <el-form
            :model="form"
            :rules="rules"
            ref="loginFormRef"
            class="form"
            @submit.prevent="onSubmit"
          >
            <el-form-item :prop="$t('电子邮件')">
              <el-input
                v-model="form.email"
                :placeholder="$t('您的电子邮件')"
                size="large"
              />
            </el-form-item>
            <el-form-item :prop="$t('密码')">
              <el-input
                v-model="form.password"
                type="password"
                :placeholder="$t('密码')"
                size="large"
                show-password
              />
            </el-form-item>
            <div class="terms">
              {{ $t("通过登录，我同意") }}
              <a @click="$router.push({ path: '/protocol', query: { id: 2 } })" class="link">{{ $t("条款") }}</a>
              {{ $t("和") }}
              <a @click="$router.push({ path: '/protocol', query: { id: 15 } })" class="link">{{ $t("隐私政策") }}</a>.
            </div>
            <el-button
              type="primary"
              class="signup-btn"
              @click="onSubmit"
              :loading="loading"
              >{{ $t("登录") }}</el-button
            >
          </el-form>
          <!-- <div class="or">OR</div> -->
          <div class="login-link">
            <a @click="$router.push('/register')" class="link">{{
              $t("注册")
            }}</a>
            <a @click="$router.push('/resetPassword')" class="link">{{
              $t("忘记密码")
            }}</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { login, userInfo2123 } from "../../api/index";
import { ElMessage } from "element-plus";
import { useI18n } from "vue-i18n";
import { useStore } from "vuex";
const store = useStore();
const { t } = useI18n();
const router = useRouter();
const loading = ref(false);
const loginFormRef = ref();

const form = reactive({
  email: "",
  password: "",
});

const rules = {
  email: [
    { required: true, message: t("请输入您的电子邮件"), trigger: "blur" },
    { type: "email", message: t("请输入有效的电子邮件"), trigger: "blur" },
  ],
  password: [
    { required: true, message: t("请输入您的密码"), trigger: "blur" },
    { min: 6, message: t("密码必须至少包含6个字符"), trigger: "blur" },
  ],
};

const onSubmit = async () => {
  if (!loginFormRef.value) return;
  try {
    await loginFormRef.value.validate();
    loading.value = true;
    const response = await login({
      username: form.email,
      password: form.password,
    });
    if (response.data) {
      let info2 = await userInfo2123();
      localStorage.setItem('userInfo', JSON.stringify(info2.data));
      store.state.userInfoK = info2.data;

      ElMessage.success(t("登录成功！"));
      router.push("/home");
    } else {
      ElMessage.error(t("登录失败，请检查您的电子邮件和密码"));
    }
  } catch (error) {
    ElMessage.error(t("登录失败，请重试"));
  } finally {
    loading.value = false;
  }
};
</script>

<style lang="scss" scoped>
.login-bg {
  min-height: 100vh;
  background-image: url("@/assets/bbbbggg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-wrapper {
  display: flex;
  width: 100%;
  max-width: 1100px;
  min-height: 600px;
  background: transparent;
  border-radius: 16px;
  box-sizing: border-box;
  padding: 32px;
  gap: 40px;
}
.login-left {
  flex: 1.2;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding-left: 32px;
  color: #222;
  .brand {
    display: flex;
    align-items: center;
    margin-bottom: 32px;
    .brand-logo {
      width: 249px;
      height: 96px;
      margin-right: 16px;
    }
    .brand-title {
      font-size: 32px;
      font-weight: 600;
      letter-spacing: 1px;
    }
  }
  .main-title {
    font-size: 32px;
    font-weight: 800;
    margin-bottom: 16px;
  }
  .sub-title {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 18px;
    line-height: 1.2;
  }
  .desc {
    font-size: 16px;
    margin-bottom: 24px;
    color: #333;
  }
  .watch-btn {
    background: #4faaff;
    color: #fff;
    border: none;
    border-radius: 24px;
    padding: 12px 32px;
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 32px;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(79, 170, 255, 0.15);
    .watch-icon {
      font-size: 20px;
      margin-right: 8px;
      vertical-align: middle;
    }
  }
  .awards {
    margin-top: 24px;
    .awards-title {
      font-size: 16px;
      font-weight: 600;
      margin-bottom: 12px;
    }
    .awards-list {
      display: flex;
      gap: 18px;
      img {
        width: 48px;
        height: 32px;
        object-fit: contain;
      }
    }
  }
}
.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  .card {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 18px;
    box-shadow: 0 8px 32px rgba(79, 170, 255, 0.1);
    padding: 40px 32px;
    min-width: 340px;
    max-width: 370px;
    display: flex;
    flex-direction: column;
    align-items: center;
    .card-title {
      font-size: 24px;
      font-weight: 700;
      margin-bottom: 12px;
      color: #222;
      text-align: center;
    }
    .welcome-bonus {
      background: #ffe6ee;
      color: #ff4d6b;
      font-size: 14px;
      font-weight: 600;
      border-radius: 12px;
      padding: 6px 18px;
      margin-bottom: 24px;
      text-align: center;
    }
    .form {
      width: 100%;
      .el-form-item {
        margin-bottom: 18px;
      }
      .el-input__wrapper {
        background: #f3f8ff;
        border-radius: 12px;
        border: 1px solid #e0e7ff;
      }
      .el-input__inner {
        font-size: 16px;
        color: #222;
      }
    }
    .terms {
      font-size: 13px;
      color: #888;
      margin-bottom: 18px;
      text-align: left;
      .link {
        color: #4faaff;
        text-decoration: underline;
        margin: 0 2px;
      }
    }
    .signup-btn {
      width: 100%;
      height: 48px;
      background: linear-gradient(90deg, #ff4d6b 0%, #ffb86b 100%);
      border: none;
      border-radius: 24px;
      color: #fff;
      font-size: 18px;
      font-weight: 700;
      margin-bottom: 18px;
      cursor: pointer;
    }
    .or {
      color: #888;
      font-size: 15px;
      margin-bottom: 10px;
      text-align: center;
    }
    .login-link {
      width: 100%;
      font-size: 15px;
      color: #888;
      display: flex;
      justify-content: space-between;
      .link {
        color: #4faaff;
        margin-left: 4px;
        cursor: pointer;
        text-decoration: underline;
      }
    }
  }
}
@media (max-width: 900px) {
  .login-wrapper {
    flex-direction: column;
    gap: 24px;
    padding: 16px;
  }
  .login-left {
    padding-left: 0;
    align-items: center;
    text-align: center;
    .brand {
      justify-content: center;
    }
    .awards-list {
      justify-content: center;
    }
  }
  .login-right {
    .card {
      min-width: 100%;
      max-width: 100%;
      padding: 32px 16px;
    }
  }
}
</style>